// index.js
// 获取应用实例
const app = getApp()
// 在页面中定义插屏广告
let interstitialAd = null
Page({
  data: {
    num: 1,
    top: [1870, 3121],
    starList: [],
    selectShow: false,
    nameStar: '',
    timer: '',
    starListnum: 1,
    scrooltype: true,
  },

  onLoad(options) {
    app.get_request('Activity/index').then(res => {
      console.log(res, "我是时间对象")
      this.setData({
        end_count_down_days: res.data.data.end_count_down_days,
        end_count_down_hours: res.data.data.end_count_down_hours,
        end_count_down_minutes: res.data.data.end_count_down_minutes,
        end_count_down_seconds: res.data.data.end_count_down_seconds,
        selection_rules: res.data.data.selection_rules,
        voting_rules: res.data.data.voting_rules,
        announcement: res.data.data.announcement,
        welfare: res.data.data.welfare
      })
      let time = res.data.data.end_count_down_seconds
      let hours = res.data.data.end_count_down_hours
      let min = res.data.data.end_count_down_minutes
      let timer = setInterval(res => {
        if (time == 0) {
          time = 60
          if (min == 0) {
            min = 60
            hours--
            if (hours == 0 && min == 0 && time == 0) {
              this.setData({
                hours: 24,
                end_count_down_days: (end_count_down_days - 1)
              })
            }
            this.setData({
              end_count_down_hours: hours
            })
          }
          min--
          this.setData({
            end_count_down_minutes: min
          })
        }
        time--
        this.setData({
          end_count_down_seconds: time
        })
      }, 1000)
      if(time===0 && hours===0 && min===0){
        clearInterval(timer)
      }
      this.setData({
        timer: timer
      })
    })
    // 广告区域
    if (wx.createInterstitialAd) {
      interstitialAd = wx.createInterstitialAd({
        adUnitId: 'adunit-faa24bbfa673096a'
      })
      interstitialAd.onLoad(() => {})
      interstitialAd.onError((err) => {})
      interstitialAd.onClose(() => {})
    }

    if (interstitialAd) {
        interstitialAd.show().catch((err) => {
          console.error(err)
        })

    }

    // 设置 页面导航条样式
    wx.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: '#000000',
      animation: {
        duration: 0,
        timingFunc: 'easeIn'
      }
    })
    let that = this
    if (options.lock == 1) {
      app.post_request('User/clockIn').then(res => {
        if (res.data.status == 200) {
          this.setData({
            modalName: 'Modal'
          })
        } else if (res.data.status == 10020) {
          wx.showToast({
            title: '今日已经打过卡了',
            icon: 'none'
          })
        }
      })
    }
    console.log(this.data.top)
    this.get_show()
    this.getvisit()
  },
  getvisit() {
    let token = wx.getStorageSync('userinfo').token
    console.log(token)
    if (token) {
      app.post_request('User/visit').then(res => {
        if (res.data.status == 200) {
          wx.showToast({
            title: '登录票数+5'
          })

        }
      })
    }

  },
  turn_web() {
    wx.navigateTo({
      url: '/pages/webView/webView?id=hyh',
    })
  },
  hideModel() {
    this.setData({
      modalName: ''
    })
  },
  open_model() {
    this.setData({
      modalName: 'houYuan'
    })
  },
  onShow: function () {
    let that = this

  },
  get_show() {
    let list = []

    let that = this
    let height = 3121
    app.get_request('Star/list').then(res => {
      console.log(res, "列表数据")

      let data = res.data.data.starList
      let number = Math.ceil(data.length / 21)
      console.log("number", number)
      for (let i = 0; i < that.data.num; i++) {
        height += 1250
        that.setData({
          top: [...that.data.top, height]
        })
        console.log(that.data.top, "什么")
      }

      for (let c in res.data.data.starList) {
        if (c == 0) {
          this.setData({
            no1id: data[0].id,
            no1name: data[0].name,
            no1vote: data[0].count,
            no1type: data[0].is_down,
            no1img: data[0].thumbnail
          })
          console.log(res.data.data.starList[0])
        } else if (c == 1) {
          this.setData({
            no2id: data[1].id,
            no2name: data[1].name,
            no2vote: data[1].count,
            no2type: data[1].is_down,
            no2img: data[1].thumbnail
          })
        } else if (c == 2) {
          this.setData({
            no3id: data[2].id,
            no3name: data[2].name,
            no3vote: data[2].count,
            no3type: data[2].is_down,
            no3img: data[2].thumbnail
          })
        } else {
          list.push(data[c])
        }
      }
      this.setData({
        starList: list
      })
      console.log(this.data.starList)


    })
  },

  select_idol(e) {
    let name = this.data.nameStar
    wx.showLoading({
      title: '搜索中...',
    })
    let token = wx.getStorageSync('userinfo').token
    if (token) {
      app.get_request('/Star/search?name=' + name).then(res => {
        wx.hideLoading()
        if (res.data.status == 10009) {
          wx.showToast({
            title: '未找到输入的明星',
            icon: 'none'
          })
        } else if (res.data.status == 200) {
          this.setData({
            selectShow: true,
            selectvote: res.data.data.count,
            selectname: res.data.data.name,
            selectType: res.data.data.is_down,
            selectImg: res.data.data.thumbnail,
            selectid: res.data.data.id
          })
        }
      })
    } else {

      wx.getUserProfile({
        desc: '获取您的用户名和头像',
        success(resu) {
          wx.login({
            success(resl) {
              console.log(resl)
              let userdata = {
                code: resl.code,
                nickName: resu.userInfo.nickName,
                avatarUrl: resu.userInfo.avatarUrl
              }
              app.post_request('Login', userdata).then(resz => {

                wx.setStorage({
                  data: resz.data.data,
                  key: 'userinfo',
                  success() {

                  }
                })
              })
            }
          })
        }
      })
    }

  },
  select(e) {
    let {
      value
    } = e.detail
    if (!value.trim()) {
      this.setData({
        selectShow: false
      })
    }
    this.setData({
      nameStar: value
    })
  },
  turn_detail(e) {

    let that = this
    let token = wx.getStorageSync('userinfo').token
    let curr = e.currentTarget.dataset
    if (token) {
      app.get_request('UserVote/index', token).then(res => {
        if (res.data.status == 10005) {
          that.getlogin(e)
        } else if (res.data.status == 200) {
          wx.navigateTo({
            url: '../starDetail/starDetail?id=' + e.currentTarget.dataset.id,
            success() {
              clearInterval(that.data.timer)
            }
          })
        }

      })
    } else {
      that.getlogin(e)
    }
    console.log(e)
  },
  getlogin(e) {
    console.log(e);
    
    let that = this
    wx.getUserProfile({
      desc: '获取您的用户名和头像',
      success(resu) {
        wx.login({
          success(resl) {
            console.log(resl)
            let userdata = {
              code: resl.code,
              nickName: resu.userInfo.nickName,
              avatarUrl: resu.userInfo.avatarUrl
            }
            app.post_request('Login', userdata).then(resz => {
              wx.setStorage({
                data: resz.data.data,
                key: 'userinfo',
                success() {
                  that.getvisit()
                  wx.navigateTo({
                    url: '../starDetail/starDetail?id=' + e.currentTarget.dataset.id,
                  })

                }
              })
            })
          }
        })
      }
    })
  },
  starList() {
    let list = this.data.starList
    let that = this
    this.data.starListnum += 1
    if (!this.data.scrooltype) {
      wx.showToast({
        title: '没有更多了',
        icon: "none"
      })
      return
    }
    wx.showLoading({
      title: '加载中...',
    })
    app.get_request('Star/list?page=' + this.data.starListnum).then(res => {
      if (res.data.data.starList.length == 0) {
        wx.showToast({
          title: '没有更多了',
          icon: 'none'
        })
        that.setData({
          scrooltype: false,
        })
      }
      console.log(res)

      let data = res.data.data.starList
      let number = Math.ceil(data.length / 21)
      let pageback = this.data.top
      console.log("number", number)
      let pagenum
      if (res.data.data.starList.length > 0) {
        for (let i = 0; i < pageback.length; i++) {
          if (i = pageback.length - 1) {
            pagenum = pageback[i] + 1250
          }
        }
        that.setData({
          top: [...that.data.top, pagenum]
        })
      }
      console.log(that.data.top, "什么")

      for (let c in res.data.data.starList) {
        list.push(data[c])
      }
      this.setData({
        starList: list
      })
      wx.hideLoading()
      console.log(this.data.starList)
    })
  },
  onReachBottom: function () {

    console.log("i")
    this.starList()
  },
  onShareAppMessage: function () {

  },

})